package serverproject;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;

import javolution.util.FastList;
import serverproject.model.Account;
import serverproject.model.Connection;
import serverproject.model.MemberList;
import serverproject.service.BanListService;
import serverproject.service.ConnectionService;

public class PerClientThread extends Thread
{
	private Socket		socket;
	private PrintWriter	writer;
	private String		name	= null;

	PerClientThread(Socket socket)
	{
		this.socket = socket;

		try
		{
			/**
			 * 불량회원으로 등록된 회원일 경우에는 접속이 불가능
			 */
			if (BanListService.getInstance().isIpBannedMember(socket))
			{
				// writer.println(" 접근이 거부된 IP입니다. 관리자에게 문의하시기 바랍니다. ");
				socket.close();
				return;
			}
			ConnectionService.getInstance().addConnectedList(socket, writer, null); // 연결된 사용자 리스트에 추가

		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void run()
	{
		try
		{
			ObjectInputStream reader = new ObjectInputStream(socket.getInputStream());
			Object object = reader.readObject();
			if(object instanceof Account)
				name = ((Account)object).getName();
			sendAll("#" + name + "님이 들어오셨습니다.");
			sendList();
			while (true)
			{
				String str = null;
				Object obj = reader.readObject();
				if(obj instanceof String)
					str = (String) obj;
				
				sendAll(name + ">" + str);
			}
		} catch (SocketException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			ConnectionService.getInstance().removeConnectedListBySocket(socket); // 연결된 사용자 리스트에서 제거
			sendAll("#" + name + "님이 나가셨습니다");
			sendList();

			try
			{
				socket.close();
			} catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void sendAll(String str)
	{		
		/**
		 * 연결된 리스트를 가져와서 for문을 통해 모든 연결된 사용자에게 대화목록을 전송해준다.
		 */
		for (Connection connection : ConnectionService.getInstance().getConnectionList())
		{
			Socket socket = connection.getSocket();
			try
			{
				ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
				oos.writeObject(str);
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	private void sendList()
	{
		for (Connection connection : ConnectionService.getInstance().getConnectionList())
		{
			Socket socket = connection.getSocket();

			try
			{
				ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
				FastList<String> tmpList = new FastList<String>();
				tmpList.add("test");
				oos.writeObject(new MemberList(tmpList));
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

}
